#ifndef __WORKQ_H_
#define __WORKQ_H_

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <queue>
#include "Task.h"

class WorkQueue {
 private:
  int num_threads;
  std::queue<Task*> workq;
  pthread_t* pool;  
  pthread_cond_t empty_cond;
  pthread_mutex_t cmutex;
  pthread_mutex_t qmutex;
  int count;
  pthread_cond_t done;
  friend void* run_thread(void*); // the function that the thread implements //
 public:
  WorkQueue () {
    count = 0;
    pthread_mutex_init(&qmutex, NULL);
    pthread_mutex_init(&cmutex, NULL);
    pthread_cond_init (&empty_cond, NULL);
    pthread_cond_init (&done, NULL);
  }
  
  ~WorkQueue () {
    wait_for_threads();
  }

  void setNumThreads(int number); // set the number of threads in the pool //

  int initThreads(); // initialize the threads in the pool //

  int addTask(Task* t); // add a new task to the queue //

  int wait_for_threads(); // wait for the threads to come back //

};

void wait_for_task(Task* t);

#endif
